/**
 * 大数据按地域统计（单日)
 * @param startTime
 * @param endTime
 * @param area
 * @param page
 */
//获取url中参数的值
var LocString=String(window.document.location.href);
 function GetQueryString(str){
	 var rs=new RegExp("(^|)"+str+"=([^&]*)(&|$)","gi").exec(LocString),tmp;
	 if(tmp=rs)return tmp[2];
	 return "没有这个参数";
 }
 
 //单天的查询
function byAreaOneDay(startTime,endTime,area,page) {
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var	area = GetQueryString("area");
	var area=unescape(area);
	var param = {
		"startTime" : startTime,
		"endTime" : endTime,
		"area" : area,
		"page" : page
	}
	var aj = $.ajax({
		url : '/web-adv/byAreaOneDay',
		data : param,
		type : 'post',
		success : function(data) {
			$('tbody > tr td').remove();
			$('#pageBox').extendPagination({
				totalCount:data.total,
		 		showPage: data.pages+1,
	            limit: data.pageSize,
	            goPage:page,
	            callback: function (page) {
	            	getPage(page);	
	            }
			});
			endTime = GetQueryString("endTime");
			startTime= GetQueryString("startTime");
			area = GetQueryString("area");
			var area=unescape(area);
			$("#endTime").val(endTime);
			$("#startTime").val(startTime);
			$("#area").val(area);

			var beans = data.beans;
			for(var j = 0; j < beans.length;j++){
				//保留2位有效数字
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var sumAdpay=parseFloat(beans[j].sumAdpay).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2)+"%";
				if(typeof beans[j].time=="object" ){
					beans[j].time="0000-00-00"
				}
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
					"<td class='time'>"+beans[j].time.split(" ")[0]+"</td>" +
					"<td class='area'>"+beans[j].area+"</td>" +
					"<td class='adNum'>"+beans[j].userNum+"</td>" +
					"<td class='aPrice'>"+averagePrice+"</td>" +
					"<td class='clickNum'>"+sumAdpay+"</td>" +
					"<td class='adIncome'>"+adPay+"</td>" +
					"<td class='allIncome'>"+proportion+"</td>" +
					"<td class='percent'>"+beans[j].budgetBalance+"</td>" +
					"<td class='rownum'>"+(j+1)+"</td></tr>";
				$("#tb").append(tbodys);
			}
		}
	});
};
/**
 * 获取后台传过来的page
 */
function  getPage(page){
	//每次点击的时候清空表格
	$('tbody > tr td').remove();
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var area = $("#area").val();
	var param = {
		"startTime" : startTime,
		"endTime" : endTime,
		"area" : area,
		"page" : page
	}
	var aj = $.ajax({
		url : '/web-adv/byAreaOneDay',
		data : param,
		type : 'post',
		success : function(data) {
			$('tbody > tr td').remove();
			$("#endTime").val(data.endTime);
			$("#startTime").val(data.startTime);
			var beans = data.beans;
			var sumAdpayNum=0;
			var budgetMoney=0;
			var adPayNum=0;
			var proportionNum=0;
			for(var j = 0; j < beans.length;j++){
				if(typeof beans[j].clickNum=="object"){
					beans[j].clickNum=0;
				}
				
				//保留2位有效数字
				if(typeof beans[j].time=="object" ){
					beans[j].time="0000-00-00"
				}
				//保留2位有效数字
				var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
				var sumAdpay=parseFloat(beans[j].sumAdpay).toFixed(2);
				var adPay=parseFloat(beans[j].adPay).toFixed(2);
				var proportion=parseFloat((beans[j].proportion)*100).toFixed(2)+"%";
				
				var tbodys = "<tr><td>"+(j+1)+"</td>" +
				"<td class='time'>"+beans[j].time.split(" ")[0]+"</td>" +
				"<td class='area'>"+beans[j].area+"</td>" +
				"<td class='adNum'>"+beans[j].userNum+"</td>" +
				"<td class='aPrice'>"+averagePrice+"</td>" +
				"<td class='clickNum'>"+sumAdpay+"</td>" +
				"<td class='adIncome'>"+adPay+"</td>" +
				"<td class='allIncome'>"+proportion+"</td>" +
				"<td class='percent'>"+beans[j].budgetBalance+"</td>" +
				"<td class='rownum'>"+beans[j].top+"</td></tr>";
				$("#tb").append(tbodys);
			}
		}
	});
};

$(function(){
	byAreaOneDay("1");
	 //byArea();
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}; 
	//每次点击的时候清空表格
	$('tbody > tr td').remove();
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
			}else{
				if(page==1){
					byAreaOneDay(page);
				}else if(page>showPageN-4){
				}else{
					byAreaOneDay(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
})
	
//返回
function turn(){
	window.location.href="/web-adv/advWeb/views/bigData/index.html?selectedNav%3D4%26navPos%3D0";
}

/**
 * 单日查看 查询
 * @param page
 */
function selectByAreaOneDay(page){
	$('tbody > tr td').remove();
	if(page==null || page.length<=0){
		var page=1;
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		var area = $("#area").val();
		var page = $('.pagination .active a').text();
		var param = {
				"startTime" : startTime,
				"endTime" : endTime,
				"area" : area,
				"page" : page,
			}
		var aj = $.ajax({
			url : '/web-adv/byArea',
			data : param,
			type : 'post',
			success : function(data) {
				$('tbody > tr td').remove();
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byAreaOneDay(page);	
		            }
				});
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);
				$("#area").val(data.area);
				var beans = data.beans;
				for(var j = 0; j < beans.length;j++){
					//保留2位有效数字
					var averagePrice=parseFloat(beans[j].averagePrice).toFixed(2);
					var sumAdpay=parseFloat(beans[j].sumAdpay).toFixed(2);
					var adPay=parseFloat(beans[j].adPay).toFixed(2);
					var proportion=parseFloat((beans[j].proportion)*100).toFixed(2)+"%";
					if(typeof beans[j].time=="object" ){
						beans[j].time="0000-00-00"
					}
					var tbodys = "<tr><td>"+(j+1)+"</td>" +
					"<td class='area'>"+beans[j].time.split(" ")[0]+"</td>" +
					"<td class='area'>"+beans[j].area+"</td>" +
					"<td class='adNum'>"+beans[j].userNum+"</td>" +
					"<td class='aPrice'>"+averagePrice+"</td>" +
					"<td class='clickNum'>"+sumAdpay+"</td>" +
					"<td class='adIncome'>"+adPay+"</td>" +
					"<td class='allIncome'>"+proportion+"</td>" +
					"<td class='percent'>"+beans[j].budgetBalance+"</td>" +
					"<td class='rownum'>"+beans[j].top+"</td></tr>";
					$("#tb").append(tbodys);
				}
			}
		})
	}
}

